<?php

/**
 * Created by PhpStorm.
 * User: d7
 * Date: 2016/1/5
 * Time: 17:12
 */
class Page_index extends MY_Admin_Controller{
    function __construct(){
        parent::__construct();


        $this->assign('index_channel','marketing');
        $this->load->model('Recommend_model');
    }
    function index($func = false){
        $this->load->helper('image');
        $this->load->config('recommend');
        $func_config = $this->config->item('func');
        $source_config = $this->config->item('source');
        $func = $this->input->get('func');
        $this->assign('func',$func_config);
        $conditions = array();
        if($func)
            $conditions['func'] = $func;
        $recommend_source = $this->input->get('recommend_type');
        //来源、字段
        $recommend = array();
        foreach($source_config as $key=>$v){
            $fields = explode(',',$v['field']);
            $conditions['source'] = $key;
            $rec = $this->Recommend_model->select_recommend($conditions,$v['source'],$fields);
            $recommend = array_merge($recommend,is_array($rec)?$rec:array());
        }
        $this->assign('recommend_source',@$recommend_source);
        $this->assign('items', $recommend);
        $this->display();
    }

    function set_recommend($id = false){
        if($id) {
            $id = intval($id);
            $recommend = $this->Recommend_model->find_one($id);
            $this->assign('recommend',$recommend);
        }

        $this->load->config('recommend');
        $func_config = $this->config->item('func');
        $source_config = $this->config->item('source');

        $this->assign('func',$func_config);
        $this->assign('source',$source_config);

        $this->display();
    }
    function do_set_recommend(){
        $func = trim($this->input->post('func'));
        $source = trim($this->input->post('source'));
        $source_id = intval($this->input->post('source_id'));
        $data = array('func'=>$func,'source'=>$source,'source_id'=>$source_id);
        $result = $this->Recommend_model->insert($data);

        if($result){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
    function do_change_recommend(){
        $id = intval(trim($this->input->post('id')));
        $func = trim($this->input->post('func'));
        $source = trim($this->input->post('source'));
        $source_id = intval($this->input->post('source_id'));
        $data = array('id'=>$id,'func'=>$func,'source'=>$source,'source_id'=>$source_id);
        $result = $this->Recommend_model->update($data,$id);

        if($result){
            $this->json_response(true);
        }
        $this->json_response(false);
    }

    public function generate_config()
    {
        $this->load->config('recommend');
        $source_config = $this->config->item('source');
        $config = array();
        //var_dump($source_config);
        foreach($source_config as $key=>$v){
//            var_dump($config);
            $config = array_merge_recursive($config,$this->_get_recommend($key,$v['source'],explode(',',$v['field'])));
        }
        //var_dump($config);
        //exit();
        $config_name = 'data/recommend';

        $this->config->load($config_name, TRUE);

        $this->config->set_item($config_name, $config);

        $this->config->save($config_name);

        $this->json_response();
    }

    private function _get_recommend($key,$source,$fields){
        $config=array();
        $conditions = array('source'=>$key);
        if($source=='activity'){
            $this->load->model('activities/Activities_model');
            $recommend = $this->Recommend_model->select_recommend($conditions,$source,$fields);
            $lecturer_ids = array();
            $place_ids = array();
            foreach($recommend as $value){
                $lecturer_ids[]  = $value['lecturer_id'];
                $place_ids[] = $value['place_id'];
            }
            $this->load->model('lecturer/Lecturer_model');
            $this->load->model('place/Place_model');
            $lecturers = $this->Lecturer_model->get_in($lecturer_ids,'id as lecturer_id,name,avatar,intro,score');
            $places = $this->Place_model->get_in($lecturer_ids,'id as place_id,name as title,cover,intro,city_id,address,traffic');
            $this->load->model('Region_model');
            foreach($places as &$v){
                $city =  $this->Region_model->getOne(array($v['city_id']));
                if(isset($city['name']))
                    $v['city'] = $city['name'];
                else
                    $v['city'] = '杭州';
                unset($v);
            }

            foreach($recommend as &$value){
                foreach($lecturers as $v){
                    if($value['lecturer_id'] == $v['lecturer_id']){
                        $value['lecturer'] = $v;
                    }
                }
                foreach($places as $v){
                    if($value['place_id'] == $v['place_id']){
                        $value['address_info'] = $v;
                    }
                }
                unset($value);
            }

            /*var_dump($recommend);
            exit();*/
        }else{
            $recommend = $this->Recommend_model->select_recommend($conditions,$source,$fields);
        }
        $i = $j = 0;

        if($recommend && is_array($recommend)){
            foreach($recommend as $val){
                if($val['func']=='index'){
                    $config[$val['func']]['recommend_'.$source][$i] = $val;
                    $i++;
                }
                if($val['func']=='content'){
                    $config[$val['func']]['recommend_'.$source][$j] = $val;
                    $j++;
                }
            }
        }
        return $config;
    }

    function del(){
        $rid = intval(trim($this->input->post('id')));
        if($rid<1){
            $this->json_response(false);
        }
        $result = $this->Recommend_model->soft_delete($rid);
        if($result){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
}